﻿/******************************************************************************
	Cobalt - utility libraries for building XNA games
	Copyright (C) 2008 Philip Djonov

	This program is free software; you can redistribute it and/or modify it
	under the terms of the GNU General Public License as published by the Free
	Software Foundation; either version 2 of the License, or (at your option)
	any later version.

	This program is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
	or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
	for more details.

	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the
	
		Free Software Foundation, Inc.
		51 Franklin Street, Fifth Floor
		Boston, MA  02110-1301, USA.
******************************************************************************/

using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;

namespace Cobalt.Content.Pipeline
{

	public class SharedResource<T>
	{
		public T Value { get; set; }
		
		public SharedResource() { }
		public SharedResource( T value )
		{
			this.Value = value;
		}
	}

	[ContentTypeWriter]
	internal class SharedResourceeWriter<T> : ContentTypeWriter<SharedResource<T>>
	{
		private ContentTypeWriter targetWriter;
		protected override void Initialize( ContentCompiler compiler )
		{
			base.Initialize( compiler );

			targetWriter = compiler.GetTypeWriter( typeof( T ) );
		}

		public override string GetRuntimeType( TargetPlatform targetPlatform )
		{
			string targetName = string.Format( "SharedResource`1[[{0}]]",
				targetWriter.GetRuntimeType( targetPlatform ) );
			
			return Helpers.GetCobaltFrameworkTypeString( targetName );
		}

		public override string GetRuntimeReader( TargetPlatform targetPlatform )
		{
			string targetName = string.Format( "SharedResourceReader`1[[{0}]]",
				targetWriter.GetRuntimeType( targetPlatform ) );

			return Helpers.GetCobaltFrameworkTypeString( targetName );
		}

		protected override void Write( ContentWriter output, SharedResource<T> value )
		{
			output.WriteSharedResource( value.Value );
		}
	}

}